package free.play;

import free.play.entity.Hobby;
import free.play.entity.User;
import free.play.repository.HobbyRepository;
import free.play.repository.UserRepository;
import free.play.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@Rollback(false)
public class ManyToManyTest2 {

  @Autowired
  private UserRepository userRepository;

  @Autowired
  private HobbyRepository hobbyRepository;

  @Autowired
  private UserService userService;

  @Test
  public void save_01() {

    User user = new User();
    Hobby hobby = new Hobby();
    user.getHobbies().add(hobby);
    userRepository.save(user);
  }

  @Test
  public void merge_01() {

    User user = new User();
    Hobby hobby = new Hobby();
    user.getHobbies().add(hobby);
    userRepository.save(user);
    userService.addHobbyToUser(user.getId(), new Hobby());
  }

  @Test
  public void merge_02() {

    User user = new User();
    Hobby hobby = new Hobby();
    user.getHobbies().add(hobby);
    userRepository.save(user);
    Hobby hobby1 = new Hobby();
    userService.addHobbyToUser(user.getId(), hobby1);
    Hobby hobby2 = new Hobby();
    userService.addHobbyToUser(user.getId(), hobby2);
    Hobby hobby3 = new Hobby();
    userService.addHobbyToUser(user.getId(), hobby3);
    userService.removeHobbyFromUser(user.getId(), hobby3);
  }

}
